<?php echo $javascript->link('filter_actions'); ?>
<div class="expenses index">
    <a href="" class="filtro_a" onclick="show_filter(); return false;"><?php echo __('Filtro', true); ?></a>
    <input type="hidden" id="input_filtro" value="0">
    <div id="filtro_busqueda" class="filtro">
        <fieldset>
            <?php echo $form->create('Expense', array('url' => array('controller' => 'expenses', 'action' => 'index'))); ?>
            <?php
            echo $form->input('expensetype_id', array('label' => 'Tipo de Gasto'));
            echo $form->input('employee_id', array('label' => 'Empleado'));
            ?>
            <div class="date_range">
                <label>Rango de Fechas</label>
                <?php
                echo $form->input('date_from', array('label' => false));
                ?>
                <span>-</span>
                <?php
                echo $form->input('date_to', array('label' => false));
                ?>
                <span><?php echo $this->Html->link(__('Hoy', true), array(), array('onclick' => 'fecha_hoy(); return false;')); ?></span>
                &nbsp;&nbsp;&nbsp;
                <span><?php echo $this->Html->link(__('Últimos 7 Días', true), array(), array('onclick' => 'fecha_semana(); return false;')); ?></span>
                <?php
                echo $form->input('hidden_actual_date', array('type' => 'hidden', 'value' => date('d/m/Y')));
                echo $form->input('hidden_week_date', array('type' => 'hidden', 'value' => date('d/m/Y', strtotime('-7 day', strtotime(date('Y-m-d H:i:s'))))));
                ?> 
            </div>
            <div class="date_range">
                <label>Rango de Fechas de Carga</label>
                <?php
                echo $form->input('created_from', array('label' => false));
                ?>
                <span>-</span>
                <?php
                echo $form->input('created_to', array('label' => false));
                ?>
                <span><?php echo $this->Html->link(__('Hoy', true), array(), array('onclick' => 'fecha_hoy_created(); return false;')); ?></span>
                &nbsp;&nbsp;&nbsp;
                <span><?php echo $this->Html->link(__('Últimos 7 Días', true), array(), array('onclick' => 'fecha_semana_created(); return false;')); ?></span>
            </div>
            <?php
            echo $form->submit(__('Filtrar', true));
            echo $form->end();
            ?>
        </fieldset>
    </div>
    <h2 class="left"><?php __('Gastos Extra'); ?></h2>
    <div class="actions specific right">
        <ul>
            <li><?php echo $this->Html->link(__('Agregar Nuevo', true), array('action' => 'add')); ?> </li>
        </ul>
    </div>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th><?php echo 'Tipo de Gasto'; ?></th>
            <th><?php echo 'Empleado'; ?></th>
            <th><?php echo 'Monto'; ?></th>
            <th><?php echo 'Fecha'; ?></th>
            <th><?php echo 'Descripción'; ?></th>
            <th><?php echo 'Fecha de Carga'; ?></th>
            <th><?php echo 'Estado'; ?></th>
            <th class="actions"><?php __('Acciones'); ?></th>
        </tr>
        <?php
        if (empty($expenses)) {
            ?> 
            <tr>
                <td colspan="7">No se encontraron Gastos</td>
            </tr>
            <?php
        }

        $i = 0;
        $monto_total = 0;
        $cant_total = 0;
        $total_pagar = 0;
        $hay_para_pagar = 0;
        foreach ($expenses as $expense):

            $monto_total += $expense['Expense']['value'];
            $cant_total++;

            $class = null;
            if ($i++ % 2 == 0) {
                $class = ' class="altrow"';
            }
            ?>
            <tr<?php echo $class; ?>>
                <td><?php echo $expense['Expensetype']['name']; ?></td>
                <td><?php echo $expense['Expense']['employee_id'] != null ? $expense['Employee']['first_name'] . ' ' . $expense['Employee']['last_name'] : 'Ninguno'; ?></td>
                <td>$<?php echo $expense['Expense']['value']; ?>&nbsp;</td>
                <td><?php echo date('d/m/Y', strtotime($expense['Expense']['date'])); ?>&nbsp;</td>
                <td><?php echo $expense['Expense']['description']; ?>&nbsp;</td>
                <td><?php echo date('d/m/Y', strtotime($expense['Expense']['created'])); ?>&nbsp;</td>
                <?php
                if ($expense['Expense']['paid'] == 0) {
                    $estado = 'No Pagado';
                    $class = 'no_paid';
                    $total_pagar += $expense['Expense']['value'];
                    $hay_para_pagar = 1;
                } else {
                    $estado = 'Pagado';
                    $class = 'paid';
                }
                ?>
                <td class="<?php echo $class; ?>"><?php echo $estado; ?>&nbsp;</td>
                <td class="actions">
                    <?php echo $this->Html->link($this->Html->image('icons/magnifier.png'), array('action' => 'view', $expense['Expense']['id']), array('title' => __('Ver', true), 'escape' => false)); ?>
                    <?php echo $this->Html->link($this->Html->image('icons/pencil.png'), array('action' => 'edit', $expense['Expense']['id']), array('title' => __('Editar', true), 'escape' => false)); ?>
                    <?php echo $this->Html->link($this->Html->image('icons/cross.png'), array('action' => 'delete', $expense['Expense']['id']), array('title' => __('Eliminar', true), 'escape' => false), sprintf(__('Seguro que desea eliminar este Gasto Extra?', true), $expense['Expense']['id'])); ?>
                    <?php
                    if ($expense['Expense']['paid'] == 0) {
                        if ($header_tipo_usuario == GROUP_ADMIN) {
                            //si es admin damos la opción para "marcar como pagado"
                            echo $this->Html->link($this->Html->image('icons/flag_blue.png'), array('action' => 'pay', $expense['Expense']['id']), array('title' => __('Marcar como Pagado', true), 'escape' => false));
                        }
                        echo $this->Html->link($this->Html->image('icons/money_dollar.png'), array('action' => 'pay', $expense['Expense']['id'], 1), array('title' => __('Pagar con Caja', true), 'escape' => false));
                    }
                    ?>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>
    <?php
    if ($hay_para_pagar) {
        ?>
        <div id="div_pagar_todo">
            <?php
            if ($header_tipo_usuario == GROUP_ADMIN) {
                //si es admin damos la opción para "marcar como pagado"
                echo $this->Html->link(__('Marcar como Pagado Todo lo que este sin pagar', true), array('controller' => 'expenses', 'action' => 'pay_filter'), array('class' => 'marcar_pagado'));
                echo '<br />';
                echo '<br />';
            }
            echo $this->Html->link(__('Pagar con Caja Todo lo que este sin pagar', true), array('controller' => 'expenses', 'action' => 'pay_filter', 1));
            ?>
        </div>
    <?php } ?>
    <div class="total_index">
        <?php
        //muestro la cantidad de gastos y el monto total
        echo $this->Form->input('cant', array('label' => 'Cantidad Total', 'value' => $cant_total, 'readonly' => 'readonly'));
        echo $this->Form->input('cant', array('label' => 'Monto Total', 'value' => '$' . $monto_total, 'readonly' => 'readonly'));
        echo $this->Form->input('cant', array('label' => 'Total a Pagar', 'value' => '$' . $total_pagar, 'readonly' => 'readonly'));
        ?>
    </div>
</div>
<script type="text/javascript" defer>
    (function($){
        // Date picker
        $("#ExpenseDateFrom").datepicker({
            defaultDate: new Date(),
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true
        });

        // Date picker
        $("#ExpenseDateTo").datepicker({
            defaultDate: new Date(),
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true
        });

        // Date picker
        $("#ExpenseCreatedFrom").datepicker({
            defaultDate: new Date(),
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true
        });

        // Date picker
        $("#ExpenseCreatedTo").datepicker({
            defaultDate: new Date(),
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true
        });

    })(jQuery);
    
    function fecha_hoy(){
        var hoy = $('#ExpenseHiddenActualDate').val();
        $('#ExpenseDateFrom').val(hoy);
        $('#ExpenseDateTo').val(hoy);
        $('#ExpenseIndexForm').submit();
    }
    
    function fecha_semana(){
        var hoy = $('#ExpenseHiddenActualDate').val();
        var semana = $('#ExpenseHiddenWeekDate').val();
        $('#ExpenseDateFrom').val(semana);
        $('#ExpenseDateTo').val(hoy);
        $('#ExpenseIndexForm').submit();
    }
    
    function fecha_hoy_created(){
        var hoy = $('#ExpenseHiddenActualDate').val();
        $('#ExpenseCreatedFrom').val(hoy);
        $('#ExpenseCreatedTo').val(hoy);
        $('#ExpenseIndexForm').submit();
    }
    
    function fecha_semana_created(){
        var hoy = $('#ExpenseHiddenActualDate').val();
        var semana = $('#ExpenseHiddenWeekDate').val();
        $('#ExpenseCreatedFrom').val(semana);
        $('#ExpenseCreatedTo').val(hoy);
        $('#ExpenseIndexForm').submit();
    }
</script>
